var   curDate=new Date();
        var   curD=curDate.getDate();
        var   curM=curDate.getMonth()+1;
        var   curY=curDate.getFullYear();
        LiChang = {
            getByClass: function (oPare, cla) {
                var oChild = oPare.getElementsByTagName("*");
                var arr = [];
                for (var i = 0; i < oChild.length; i++) {
                    var arrCla = oChild[i].className.split(" ");
                    var j;
                    for (var j in arrCla) {
                        if (arrCla[j] == cla) {
                            arr.push(oChild[i]);
                            break;
                        }
                    }
                }
                return arr;
            }
        }
        function showDate(option) {
            this.obj = document.getElementById(option.id);
        }
        showDate.prototype.init = function () {
            var _self = this;
            _self.dateText = LiChang.getByClass(_self.obj, "showDate")[0];
            _self.dateBox = LiChang.getByClass(_self.obj, "sel_date")[0];
            _self.yearBox = LiChang.getByClass(_self.obj, "year")[0];
            _self.mnBox = LiChang.getByClass(_self.obj, "month")[0];

            _self.dataTable = LiChang.getByClass(_self.dateBox, "data_table")[0];
            _self.tbody = _self.dataTable.tBodies[0];
            _self.td = _self.tbody.getElementsByTagName("td");
            _self.prevM = LiChang.getByClass(_self.dateBox, "prev_m")[0];
            _self.nextM = LiChang.getByClass(_self.dateBox, "next_m")[0];
            _self.prevY = LiChang.getByClass(_self.dateBox, "prev_y")[0];
            _self.nextY = LiChang.getByClass(_self.dateBox, "next_y")[0];

            //显示日历
            _self.show();
            _self.showNow();
            _self.banVoid();
        
            //点击选择日期
            for (var i = 0; i < _self.td.length; i++) {
                _self.td[i].onclick = function () {
                    var newDd = this.innerHTML;
                    var newYear = parseInt(_self.yearBox.value, 10);
                    var newMn = parseInt(_self.mnBox.value, 10);
                    if (newDd.match(/^\s{0}$/g)) {  //如果td有值;
                        return false;
                    }
                    if (isNaN(newYear) || isNaN(newMn) || newYear < 1900 || newYear > 2099 || newMn < 1 || newMn > 12) {  //如果td有值;
                        return false;
                    }
/*
***********************************************************************************************************
***********************************************************************************************************
*/
                    var selected_date = this.id;//组装日期

                    var   d=new   Date(Date.parse(selected_date.replace(/-/g,"/")));
                    // var   curDate=new   Date();
                    // alert(curDate);

                    if(d <= curDate){
                        alert("请选择大于今天的游玩日期！");
                        _self.dateText.value = "请选择游玩日期";
                        // this.className+="hover";
                    }else{
                        _self.dateText.value = selected_date;//输出选中日期
                    }

                    
                    // _self.dateBox.className += " ";
                    // _self.dateBox.className += "dn";
                }
                _self.td[i].onmouseover = function () {
                    if (this.className.indexOf("hove") == -1) {
                        this.className += " hover";
                    }
                }
                _self.td[i].onmouseout = function () {
                    this.className = this.className.replace("hover", "");
                }
            }

            //点击切换月份
            _self.prevM.onclick = _self.nextM.onclick = function () {
                _self.changeMn(this);
                return this;
            }
            _self.yearBox.onkeyup = _self.mnBox.onkeyup = function () {
                this.value = this.value.replace(/\D/g, "");
                var year = parseInt(_self.yearBox.value, 10);
                var month = parseInt(_self.mnBox.value, 10);
                if (!isNaN(year) && year <= 2099 && year >= 1900 && !isNaN(month) && month <= 12 && month >= 1) {
                    _self.showAllDay(year, month - 1);
                }
                if (this == _self.yearBox && year >= 1900 && year <= 2099) {
                    _self.mnBox.focus();
                } else if (this == _self.mnBox && (month < 1 || month > 12)) {
                    if (this.value.slice(0, 1) > 1) { //如果第一位大于1
                        this.value = this.value.slice(0, 1);
                    } else if (month > 12) { //
                        this.value = 12;
                    }
                    year = parseInt(_self.yearBox.value, 10);
                    month = parseInt(_self.mnBox.value, 10);
                    _self.showAllDay(year, month - 1);
                }
            }
            _self.yearBox.onblur = function () {
                var year = parseInt(this.value, 10);
                if (year < 1900 || year > 2099 || isNaN(year)) {
                    this.focus();
                    alert("请输入正确年份！");
                    this.value = "";
                }
            }
            _self.mnBox.onfocus = function () {
                var year = parseInt(_self.yearBox.value, 10);
                if (isNaN(year)) {
                    _self.yearBox.focus();
                }
            }
            _self.mnBox.onblur = function () {
                var month = parseInt(this.value, 10);
                var year = parseInt(_self.yearBox.value, 10);
                if (month < 1 || month > 12 || isNaN(month)) {
                    alert("请输入正确月份！");
                    this.value = "";
                }
            }
        }

        //点击切换月份
        showDate.prototype.changeMn = function (obj) {
            var _self = this;
            var NewMn = parseInt(_self.mnBox.value, 10);
            var newYear = parseInt(_self.yearBox.value, 10);
            if (isNaN(NewMn) || isNaN(newYear)) {
                alert("请填写正确的年和月！");
                return false;
            }
            if (obj == _self.nextM) {
                NewMn++;
            } else {
                NewMn--;
            }
            if (NewMn < 1) {
                NewMn = 12;
                newYear -= 1;
            } else if (NewMn > 12) {
                NewMn = 1;
                newYear += 1;
            }
            _self.mnBox.value = NewMn;
            _self.showNow(newYear, NewMn);
        }
        //显示日历
        showDate.prototype.show = function () {
            var _self = this;
            if (_self.dateBox.className.indexOf("dn") != -1) {
                var cls = _self.dateBox.className;
                _self.dateBox.className = cls.replace("dn", "");
            }
        }
        //填充年、月
        showDate.prototype.showNow = function (yr, mn, date) {
            var _self = this;
            var now = new Date();
            var year = yr || now.getFullYear();
            var month = mn - 1 || now.getMonth();
            var dd = date || now.getDate();
            //填充 年 和 月
            _self.yearBox.value = year;
            _self.mnBox.value = mn || now.getMonth() + 1;
            //填充日期
            _self.showAllDay(year, month, dd);
        }
        //填充当月的所有日期
        
        showDate.prototype.showAllDay = function (Yr, Mn, Dd) {

            var _self = this;
            var arr31 = [1, 3, 5, 7, 8, 10, 12];
            var is31 = true;
            var newDd = new Date();  //根据传入的数值生成新的日期
            Dd = Dd ? Dd : newDd.getDate();
            newDd.setFullYear(Yr, Mn, Dd);
            var year = newDd.getFullYear(),
                month = newDd.getMonth(),
                dd = newDd.getDate();

            var firstD = new Date();
            firstD.setFullYear(year, month, 1);
            var firstDay = firstD.getDay();

            var str31 = arr31.join(",");
            var regExp = eval("/" + (month + 1) + ",|," + (month + 1) + ",|," + (month + 1) + "/g");
            var dayLen = 31;
            //判断每个月有多少天
            if (str31.search(regExp) == -1) {
                dayLen = 30;
            }
            //清空日期
            for (var i = 0; i < _self.td.length; i++) {
                _self.td[i].innerHTML = "";
                 _self.td[i].id = "";
                  _self.td[i].title = "";
                  _self.td[i].style.pointerEvents="none";
                _self.td[i].className = _self.td[i].className.replace("active", "");
            }
            //如果有31天
            for (var j = 0; j < dayLen; j++) {
                var year_val = data_value.year_value.value;//获得当前年份
                var month_val = data_value.month_value.value;//获得当前月份

                //判断闰年与否，填充2月份日期
                if((year_val % 4 == 0) && (year_val % 100 != 0 || year_val % 400 == 0) && (month_val == 2) && (j == 29)){
                    break;
                }else if((year_val % 4 != 0) && (year_val % 100 == 0 || year_val % 400 != 0) && (month_val == 2) && (j == 28)){
                    break;
                }

                //设置每个日期唯一的id，即当天日期
                _self.td[j + firstDay].id = (year_val)+"-"+(month_val)+"-"+(j + 1);



                _self.td[j + firstDay].innerHTML = j + 1;//填充日期

// *************************************************************************************************************
// *************************************************************************************************************
                /*
                    *把过期时间禁用掉
                    *并设置一定透明度
                */
                var   d1=new   Date(Date.parse(_self.td[j + firstDay].id.replace(/-/g,"/")));
                if(d1 <= curDate){
                    _self.td[j + firstDay].style.pointerEvents="none";
                    _self.td[j + firstDay].style.opacity=0.4;
                    _self.td[j + firstDay].title=-1;;
                }else{
                    _self.td[j + firstDay].style.pointerEvents="auto";
                    _self.td[j + firstDay].style.opacity=1;
                    _self.td[j + firstDay].title=1;;
                }
/*
    ****************************************************************************************************************
    ****************************************************************************************************************
    *添加价格
*/
                
                //1:根据日期增加价格，可把后台传过来的json解码后的数据放到"2016-12-23"中，如："json[j][date]"
                if(_self.td[j + firstDay].id=="2016-12-26"){
                    //2:对应1中把当天价格放到j中，如：json[j][price]
                    _self.td[j + firstDay].innerHTML += "<p>&yen;"+j+"</p>";
                }

                

                // if (curY == year_val && curM == month_val && j + 1 == dd && _self.td[j + firstDay].className.indexOf("active") == -1) {
                //     _self.td[j + firstDay].className += " ";
                //     _self.td[j + firstDay].className += "active";
                // }
            }
        }

        // 禁用空格
        showDate.prototype.banVoid = function (){
            var _self = this;

            for (var i = 0; i < _self.td.length; i++) {
                var title=_self.td[i].title;
                if(title == ""){
                    _self.td[i].style.pointerEvents="none";
                }
            }
                
        }

        //函数调用
        window.onload = function () {

            var showDate1 = new showDate({ id: "data_box" });
            showDate1.init();
        }
